package com.esm.view;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.esm.app.MyApplication;
import com.esm.netity.Badlynetity;
import com.esm.netity.Orderinfo;
import com.esm.tool.BaseActivity;
import com.esm.tool.PadAdapter;
import com.esm.tool.PoAdapter;
import com.esm.tool.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends BaseActivity {

    public static final int CHANGEPO_BG = 1318;
    private final String url = MyApplication.SERVERPATH + "/Y502/GetBadlynetitywz";
    private final String url2 = MyApplication.SERVERPATH + "/Y502/Pad2getorder";
    private final String surl = MyApplication.SERVERPATH + "/Y502/InsertBadlywz";
    private final String surl2 = MyApplication.SERVERPATH + "/Y502/UpdatePriority";
    private final ArrayList<Badlynetity> mData = new ArrayList<Badlynetity>();
    private final ArrayList<Badlynetity> mData0 = new ArrayList<Badlynetity>();
    private final ArrayList<Badlynetity> mData1 = new ArrayList<Badlynetity>();
    private final ArrayList<Badlynetity> mData2 = new ArrayList<Badlynetity>();
    private final ArrayList<Badlynetity> mData3 = new ArrayList<Badlynetity>();
    private final ArrayList<Badlynetity> mData4 = new ArrayList<Badlynetity>();
    private final ArrayList<Badlynetity> mData5 = new ArrayList<Badlynetity>();
    //private ArrayList<Integer> list=new ArrayList<Integer>();
    private final boolean bl = false;
    private final ArrayList<Orderinfo> order = new ArrayList<Orderinfo>();
    RequestQueue mQueue = null;
    JsonObjectRequest mJsonObjectRequest = null;
    StringRequest stringRequest = null;
    String riqi = "";
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    private GridView gv;
    private Spinner cx;
    private TextView zjs;
    private TextView ycs;
    private TextView cd;
    private TextView zc;
    private TextView zd;
    private TextView cx2;
    private TextView cl;
    private TextView qt;
    private PadAdapter adapter;
    private PoAdapter adapter1;
    //private ImageView image;
    private TextView qr;
    private TextView cz;
    private int yichang, jianchu;//
    private String sn, style, po;
    private String nnn = "";
    private int dqlx = 0;//当前类型
    private TextView xtl;
    private TextView xtr;
    private TextView qwl;
    private TextView qwr;
    private TextView qnl;
    private TextView qnr;
    private TextView xwl;
    private TextView xwr;
    private TextView xnl;
    private TextView xnr;
    private TextView xgl;
    private TextView xgr;
    private int dqbl = -1;//当前的那个不良代码被点了
    private String dqwz = " ";//当前那个位置被点了
    private int len = 0;
    private Badlynetity a;
    private TextView pz;
    private LinearLayout bg;
    public Handler _handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (CHANGEPO_BG == msg.what) {
                onChangePoBG();
            }
        }
    };
    private long firstTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //无title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_pad);
        String sta = MyApplication.yy;
        updateActivity(sta, MainActivity.class);

        long thistime = MyApplication.xtime * 1000 + System.currentTimeMillis();
        riqi = df.format(new Date(thistime));
        mQueue = MyApplication.getRequestQueue();
        sn = getIntent().getStringExtra("sn");
        nnn = getIntent().getStringExtra("nnn");
        jianchu = Integer.parseInt(nnn);
        initView();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mQueue = MyApplication.getRequestQueue();
    }

    private void initView() {
        gv = (GridView) findViewById(R.id.gv);
        cx = (Spinner) findViewById(R.id.pad_cx);
        zjs = (TextView) findViewById(R.id.Pad_zjs);
        ycs = (TextView) findViewById(R.id.pad_ycs);
        pz = (TextView) findViewById(R.id.tv_line);
        qr = (TextView) findViewById(R.id.pad_qr);
        cz = (TextView) findViewById(R.id.pad_cz);
        cd = (TextView) findViewById(R.id.pad_cd);
        zc = (TextView) findViewById(R.id.pad_zc);
        zd = (TextView) findViewById(R.id.pad_zd);
        cx2 = (TextView) findViewById(R.id.pad_cx2);
        cl = (TextView) findViewById(R.id.pad_cl);
        qt = (TextView) findViewById(R.id.pad_qt);
        xtl = (TextView) findViewById(R.id.pad_xtl);
        xtl.setOnClickListener(this);
        xtr = (TextView) findViewById(R.id.pad_xtr);
        xtr.setOnClickListener(this);
        qwl = (TextView) findViewById(R.id.pad_qwl);
        qwl.setOnClickListener(this);
        qwr = (TextView) findViewById(R.id.pad_qwr);
        qwr.setOnClickListener(this);
        qnl = (TextView) findViewById(R.id.pad_qnl);
        qnl.setOnClickListener(this);
        qnr = (TextView) findViewById(R.id.pad_qnr);
        qnr.setOnClickListener(this);
        xwl = (TextView) findViewById(R.id.pad_xwl);
        xwr = (TextView) findViewById(R.id.pad_xwr);
        xnl = (TextView) findViewById(R.id.pad_xnl);
        xnr = (TextView) findViewById(R.id.pad_xnr);
        xgl = (TextView) findViewById(R.id.pad_xgl);
        xgr = (TextView) findViewById(R.id.pad_xgr);
        bg = (LinearLayout) findViewById(R.id.mian_bg);
        if (!MyApplication.path.equals(""))
            showImage(MyApplication.path);
        pz.setOnClickListener(this);
        xwl.setOnClickListener(this);
        xwr.setOnClickListener(this);
        xnl.setOnClickListener(this);
        xnr.setOnClickListener(this);
        xgl.setOnClickListener(this);
        xgr.setOnClickListener(this);
        getData();
        adapter = new PadAdapter(this, mData);
        gv.setAdapter(adapter);
        gv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                setbtn2(dqwz, false);
                if (mData.get(position).getZt().equals("1"))//if(list.contains((Integer)position))
                {
                    dqwz = mData.get(position).getWeizhi();//显示位置
                    dqbl = position;
                    setbtn2(dqwz, true);
                } else {
                    RelativeLayout rl = (RelativeLayout) gv.getAdapter().getView(position, view, parent);
                    FrameLayout fl = (FrameLayout) rl.getChildAt(1);
                    TextView tv = (TextView) fl.getChildAt(1);
                    tv.setVisibility(View.VISIBLE);
                    mData.get(position).setZt("1");
                    mData.get(position).setWeizhi(lastWeizhi);
                    dqbl = position;
                }
            }
        });
        gv.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                //RelativeLayout rl=(RelativeLayout)gv.getAdapter().getView(position, view, parent);
                // FrameLayout fl=(FrameLayout)rl.getChildAt(1);
                // TextView tv=(TextView)fl.getChildAt(1);
                //  tv.setVisibility(View.GONE);
                //  setbtn2(dqwz,false);
                String name = mData.get(position).getName();
                try {
                    name = URLEncoder.encode(name, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                a = mData.get(position);
                if (dqlx == 0) {
                    updatepriority(name, "0");
                } else {
                    boolean ln = mData0.contains(a);
                    if (!ln)
                        updatepriority(name, "1");//mData0 添加
                    Utils.showToast(getString(R.string.yjcz));

                }
                // dqwz=mData.get(position).getWeizhi();//显示位置//取消位置
                // mData.get(position).setWeizhi(" ");
                // setbtn2(dqwz,false);
                // dqwz=" ";
                dqbl = -1;
                return true;
            }
        });
        cd.setOnClickListener(this);
        zc.setOnClickListener(this);
        zd.setOnClickListener(this);
        cx2.setOnClickListener(this);
        cl.setOnClickListener(this);
        qt.setOnClickListener(this);
        cz.setOnClickListener(this);
        qr.setOnClickListener(this);
        ycs.setOnClickListener(this);
        zjs.setOnClickListener(this);
        zjs.setText(nnn + "");
        adapter1 = new PoAdapter(this, order);
        cx.setAdapter(adapter1);
        getData2();
        cx.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                po = order.get(position).getPo();
                style = order.get(position).getStyle();
                _handler.sendEmptyMessage(CHANGEPO_BG);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void onChangePoBG() {

        String imagePath = MyApplication.path;

        if (po.equals("ESM001")) {
            //A02810001
            bg.setBackgroundResource(R.drawable.nike);
        } else if (po.equals("ESM002")) {
            //36818506
            bg.setBackgroundResource(R.drawable.puma);
        } else if (po.equals("ESM003")) {
            //F36392
            bg.setBackgroundResource(R.drawable.adi);
        } else {
            showImage(imagePath);
        }
    }

    private void setData(String url) {

        boolean isConnected = Utils.toastIsNetworkConnected(context);
        if (isConnected) {
            //showProgressBar();
            mJsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url,
                    null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    //hideProgressBar();
                    parseData1(response);
                }


            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //hideProgressBar();
                    len = 0;
                    jianchu--;
                    Utils.showToast(getString(R.string.network_error_try_again));
                }
            }
            ) {

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("Charset", "utf-8");
                    headers.put("Content-Type", "application/x-javascript");
                    headers.put("Accept-Encoding", "gzip,deflate");
                    return headers;
                }

            };
            mQueue.add(mJsonObjectRequest);
        } else {
            len = 0;
            Utils.showToast(getString(R.string.network_error_try_again));
            return;
        }
    }

    public void parseData1(JSONObject response) {
        String re = null;
        try {
            re = response.getString("result");

            if (re.equals("ok")) {
                runOnUiThread(new Runnable() {
                    public void run() {


                        nnn = jianchu + "";
                        yichang = yichang + len;
                        zjs.setText(jianchu + "");
                        ycs.setText(yichang + "");
                        for (Badlynetity b : mData0) {
                            if (b.getZt().equals("1")) {
                                b.setNum(b.getNum() + 1);
                                b.setZt("0");
                                b.setWeizhi(" ");
                            }
                        }
                        for (Badlynetity b : mData1) {
                            if (b.getZt().equals("1")) {
                                b.setNum(b.getNum() + 1);
                                b.setZt("0");
                                b.setWeizhi(" ");
                            }
                        }
                        for (Badlynetity b : mData2) {
                            if (b.getZt().equals("1")) {
                                b.setNum(b.getNum() + 1);
                                b.setZt("0");
                                b.setWeizhi(" ");
                            }
                        }
                        for (Badlynetity b : mData3) {
                            if (b.getZt().equals("1")) {
                                b.setNum(b.getNum() + 1);
                                b.setZt("0");
                                b.setWeizhi(" ");
                            }
                        }
                        for (Badlynetity b : mData4) {
                            if (b.getZt().equals("1")) {
                                b.setNum(b.getNum() + 1);
                                b.setZt("0");
                                b.setWeizhi(" ");
                            }
                        }
                        for (Badlynetity b : mData5) {
                            if (b.getZt().equals("1")) {
                                b.setNum(b.getNum() + 1);
                                b.setZt("0");
                                b.setWeizhi(" ");
                            }
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
            } else {
                jianchu--;
            }
            len = 0;
            Utils.showToast(re);
            //toastFullScreen(re);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private void updatepriority(String name, String lei) {
        boolean isConnected = Utils.toastIsNetworkConnected(context);
        if (isConnected) {
            String uu = surl2 + "?name=" + name + "&&lei=" + lei;
            mJsonObjectRequest = new JsonObjectRequest(Request.Method.POST, uu,
                    null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    parseData3(response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Utils.showToast(getString(R.string.network_error_try_again));
                }
            }
            );
            mQueue.add(mJsonObjectRequest);
        } else {
            Utils.showToast(getString(R.string.network_error_try_again));
            return;
        }
    }

    public void parseData3(final JSONObject response) {

        runOnUiThread(new Runnable() {
            public void run() {
                String re = null;
                try {
                    re = response.getString("result");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if (re.equals("yes")) {
                    if (dqlx == 0) {
                        mData.remove(a);
                        mData0.remove(a);
                        adapter.notifyDataSetChanged();
                        Utils.showToast(getString(R.string.sccg));

                    } else {
                        mData0.add(a);
                        Utils.showToast(getString(R.string.tjcg));
                    }
                } else {
                    Utils.showToast(getString(R.string.tjsb));
                }
            }
        });
    }

    private void getData2() {
        boolean isConnected = Utils.toastIsNetworkConnected(context);
        if (isConnected) {
            String uu = url2 + "?riqi=" + riqi;
            mJsonObjectRequest = new JsonObjectRequest(Request.Method.POST, uu,
                    null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    parseData2(response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Utils.showToast(getString(R.string.network_error_try_again));
                }
            }
            );
            mQueue.add(mJsonObjectRequest);
        } else {
            Utils.showToast(getString(R.string.network_error_try_again));
            return;
        }
    }

    public void parseData(final JSONObject response) {

        runOnUiThread(new Runnable() {
            public void run() {
                mData.clear();
                String re = null;
                try {
                    re = response.getString("result");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (re.equals("yes")) {
                    JSONArray array = response.optJSONArray("data");
                    int k = 0;
                    if (array != null) {
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject obj = array.optJSONObject(i);
                            String del = obj.optString("del");
                            k++;
                            Badlynetity b = new Badlynetity();
                            b.setCode(k + "");
                            b.setName(obj.optString("name"));
                            b.setNameen(obj.optString("nameen"));
                            b.setNamevi(obj.optString("namevi"));
                            String lei = obj.optString("leixing");
                            b.setLeixing(lei);
                            b.setZt("0");
                            b.setWeizhi(" ");
                            int n = obj.optInt("num");
                            b.setNum(n);
                            yichang = yichang + n;
                            switch (lei) {
                                case "裁断":
                                    mData1.add(b);
                                    break;
                                case "针车":
                                    mData2.add(b);
                                    break;
                                case "组底":
                                    mData3.add(b);
                                    break;
                                case "成型":
                                    mData4.add(b);
                                    break;
                                case "线外加工":
                                    mData5.add(b);
                                    break;
                            }
                            if (del.equals("1")) {
                                mData0.add(b);
                                mData.add(b);
                            }

                        }
                    }
                    ycs.setText(yichang + "");
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    private void getData() {

        boolean isConnected = Utils.toastIsNetworkConnected(context);
        if (isConnected) {
            String uu = url + "?sn=" + sn + "&&riqi=" + riqi;
            mJsonObjectRequest = new JsonObjectRequest(Request.Method.POST, uu,
                    null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    parseData(response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Utils.showToast(getString(R.string.network_error_try_again));
                }
            }
            );
            mQueue.add(mJsonObjectRequest);
        } else {
            Utils.showToast(getString(R.string.network_error_try_again));
            return;
        }
    }

    public void parseData2(final JSONObject response) {

        runOnUiThread(new Runnable() {
            public void run() {
                order.clear();
                MyApplication.slist.clear();
                String re = null;
                try {
                    re = response.getString("result");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if (re.equals("yes")) {
                    JSONArray array = response.optJSONArray("data");

                    if (array != null) {
                        for (int i = 0; i < array.length(); i++) {
                            Orderinfo o = new Orderinfo();
                            JSONObject obj = array.optJSONObject(i);
                            o.setPo(obj.optString("po"));
                            o.setStyle(obj.optString("style"));
                            o.setNum(obj.optInt("num"));
                            MyApplication.slist.add(obj.optString("po"));
                            order.add(o);
                        }
                    }
                    adapter1.notifyDataSetChanged();
                }
            }
        });
    }

    private void setbtn2(String s, boolean l) {
        if (s.equals(""))
            return;
        switch (s) {
            case "左鞋头":
                if (l)
                    xtl.setBackgroundColor(Color.parseColor("#66ffff00"));
                else
                    xtl.setBackgroundColor(Color.parseColor("#00000000"));
                break;
            case "左前掌外腰":
                if (l)
                    qwl.setBackgroundColor(Color.parseColor("#66ffff00"));
                else
                    qwl.setBackgroundColor(Color.parseColor("#00000000"));
                break;
            case "左前掌内腰":
                if (l)
                    qnl.setBackgroundColor(Color.parseColor("#66ffff00"));
                else
                    qnl.setBackgroundColor(Color.parseColor("#00000000"));
                break;
            case "右前掌外腰":
                if (l)
                    qwr.setBackgroundColor(Color.parseColor("#66ffff00"));
                else
                    qwr.setBackgroundColor(Color.parseColor("#00000000"));
                break;
            case "右前掌内腰":
                if (l)
                    qnr.setBackgroundColor(Color.parseColor("#66ffff00"));
                else
                    qnr.setBackgroundColor(Color.parseColor("#00000000"));
                break;
            case "右鞋头":
                if (l)
                    xtr.setBackgroundColor(Color.parseColor("#66ffff00"));
                else
                    xtr.setBackgroundColor(Color.parseColor("#00000000"));
                break;
            case "左鞋身内腰":
                if (l)
                    xnl.setBackgroundColor(Color.parseColor("#66ffff00"));
                else
                    xnl.setBackgroundColor(Color.parseColor("#00000000"));
                break;
            case "右鞋身内腰":
                if (l)
                    xnr.setBackgroundColor(Color.parseColor("#66ffff00"));
                else
                    xnr.setBackgroundColor(Color.parseColor("#00000000"));
                break;
            case "左鞋身外腰":
                if (l)
                    xwl.setBackgroundColor(Color.parseColor("#66ffff00"));
                else
                    xwl.setBackgroundColor(Color.parseColor("#00000000"));
                break;
            case "右鞋身外腰":
                if (l)
                    xwr.setBackgroundColor(Color.parseColor("#66ffff00"));
                else
                    xwr.setBackgroundColor(Color.parseColor("#00000000"));
                break;
            case "右鞋跟":
                if (l)
                    xgr.setBackgroundColor(Color.parseColor("#66ffff00"));
                else
                    xgr.setBackgroundColor(Color.parseColor("#00000000"));
                break;
            case "左鞋跟":
                if (l)
                    xgl.setBackgroundColor(Color.parseColor("#66ffff00"));
                else
                    xgl.setBackgroundColor(Color.parseColor("#00000000"));
        }
    }

    private void setbtnys() {
        switch (dqlx) {
            case 0:
                cd.setBackgroundResource(R.drawable.shape_bg2);
                break;
            case 1:
                zc.setBackgroundResource(R.drawable.shape_bg2);
                break;
            case 2:
                zd.setBackgroundResource(R.drawable.shape_bg2);
                break;
            case 3:
                cx2.setBackgroundResource(R.drawable.shape_bg2);
                break;
            case 4:
                cl.setBackgroundResource(R.drawable.shape_bg2);
                break;
            default:
                qt.setBackgroundResource(R.drawable.shape_bg2);
        }
    }

    String lastWeizhi = "";

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pad_xtl:
                if (!dqwz.equals(" ")) {
                    setbtn2(dqwz, false);
                }
                if (dqbl >= 0) {
                    dqwz = "左鞋头";
                    setbtn2(dqwz, true);
                    lastWeizhi = dqwz;
                    mData.get(dqbl).setWeizhi("左鞋头");
                }
                break;
            case R.id.pad_xtr:
                if (!dqwz.equals(" ")) {
                    setbtn2(dqwz, false);
                }
                if (dqbl >= 0) {
                    dqwz = "右鞋头";
                    setbtn2(dqwz, true);
                    lastWeizhi = dqwz;
                    mData.get(dqbl).setWeizhi("右鞋头");
                }
                break;
            case R.id.pad_qnl:
                if (!dqwz.equals(" ")) {
                    setbtn2(dqwz, false);
                }
                if (dqbl >= 0) {
                    dqwz = "左前掌内腰";
                    setbtn2(dqwz, true);
                    lastWeizhi = dqwz;
                    mData.get(dqbl).setWeizhi("左前掌内腰");
                }
                break;
            case R.id.pad_qnr:
                if (!dqwz.equals(" ")) {
                    setbtn2(dqwz, false);
                }
                if (dqbl >= 0) {
                    dqwz = "右前掌内腰";
                    setbtn2(dqwz, true);
                    lastWeizhi = dqwz;
                    mData.get(dqbl).setWeizhi("右前掌内腰");
                }
                break;
            case R.id.pad_qwl:
                if (!dqwz.equals(" ")) {
                    setbtn2(dqwz, false);
                }
                if (dqbl >= 0) {
                    dqwz = "左前掌外腰";
                    setbtn2(dqwz, true);
                    lastWeizhi = dqwz;
                    mData.get(dqbl).setWeizhi("左前掌外腰");
                }
                break;
            case R.id.pad_qwr:
                if (!dqwz.equals(" ")) {
                    setbtn2(dqwz, false);
                }
                if (dqbl >= 0) {
                    dqwz = "右前掌外腰";
                    setbtn2(dqwz, true);
                    lastWeizhi = dqwz;
                    mData.get(dqbl).setWeizhi("右前掌外腰");
                }
                break;
            case R.id.pad_xwl:
                if (!dqwz.equals(" ")) {
                    setbtn2(dqwz, false);
                }
                if (dqbl >= 0) {
                    dqwz = "左鞋身外腰";
                    setbtn2(dqwz, true);
                    lastWeizhi = dqwz;
                    mData.get(dqbl).setWeizhi("左鞋身外腰");
                }
                break;
            case R.id.pad_xwr:
                if (!dqwz.equals(" ")) {
                    setbtn2(dqwz, false);
                }
                if (dqbl >= 0) {
                    dqwz = "右鞋身外腰";
                    setbtn2(dqwz, true);
                    lastWeizhi = dqwz;
                    mData.get(dqbl).setWeizhi("右鞋身外腰");
                }
                break;
            case R.id.pad_xnl:
                if (!dqwz.equals(" ")) {
                    setbtn2(dqwz, false);
                }
                if (dqbl >= 0) {
                    dqwz = "左鞋身内腰";
                    setbtn2(dqwz, true);
                    lastWeizhi = dqwz;
                    mData.get(dqbl).setWeizhi("左鞋身内腰");
                }
                break;
            case R.id.pad_xnr:
                if (!dqwz.equals(" ")) {
                    setbtn2(dqwz, false);
                }
                if (dqbl >= 0) {
                    dqwz = "右鞋身内腰";
                    setbtn2(dqwz, true);
                    lastWeizhi = dqwz;
                    mData.get(dqbl).setWeizhi("右鞋身内腰");
                }
                break;
            case R.id.pad_xgl:
                if (!dqwz.equals(" ")) {
                    setbtn2(dqwz, false);
                }
                if (dqbl >= 0) {
                    dqwz = "左鞋跟";
                    setbtn2(dqwz, true);
                    lastWeizhi = dqwz;
                    mData.get(dqbl).setWeizhi("左鞋跟");
                }
                break;
            case R.id.pad_xgr:
                if (!dqwz.equals(" ")) {
                    setbtn2(dqwz, false);
                }
                if (dqbl >= 0) {
                    dqwz = "右鞋跟";
                    setbtn2(dqwz, true);
                    lastWeizhi = dqwz;
                    mData.get(dqbl).setWeizhi("右鞋跟");
                }
                break;
            case R.id.pad_cd:
                if (dqlx != 0) {
                    mData.clear();
                    mData.addAll(mData0);
                    adapter.notifyDataSetChanged();
                    cd.setBackgroundResource(R.drawable.shape_bg5);
                    setbtnys();
                    dqlx = 0;
                    dqbl = -1;
                    if (!dqwz.equals(" ")) {
                        setbtn2(dqwz, false);
                        dqwz = " ";
                    }
                }
                break;
            case R.id.pad_zc:
                if (dqlx != 1) {
                    mData.clear();
                    mData.addAll(mData1);
                    adapter.notifyDataSetChanged();
                    zc.setBackgroundResource(R.drawable.shape_bg5);
                    setbtnys();
                    dqlx = 1;
                    dqbl = -1;
                    if (!dqwz.equals(" ")) {
                        setbtn2(dqwz, false);
                        dqwz = " ";
                    }
                }
                break;
            case R.id.pad_zd:
                if (dqlx != 2) {
                    mData.clear();
                    mData.addAll(mData2);
                    adapter.notifyDataSetChanged();
                    zd.setBackgroundResource(R.drawable.shape_bg5);
                    setbtnys();
                    dqlx = 2;
                    dqbl = -1;
                    if (!dqwz.equals(" ")) {
                        setbtn2(dqwz, false);
                        dqwz = " ";
                    }
                }
                break;
            case R.id.pad_cx2:
                if (dqlx != 3) {
                    mData.clear();
                    mData.addAll(mData3);
                    adapter.notifyDataSetChanged();
                    cx2.setBackgroundResource(R.drawable.shape_bg5);
                    setbtnys();
                    dqlx = 3;
                    dqbl = -1;
                    if (!dqwz.equals(" ")) {
                        setbtn2(dqwz, false);
                        dqwz = " ";
                    }
                }
                break;
            case R.id.pad_cl:
                if (dqlx != 4) {
                    mData.clear();
                    mData.addAll(mData4);
                    adapter.notifyDataSetChanged();
                    cl.setBackgroundResource(R.drawable.shape_bg5);
                    setbtnys();
                    dqlx = 4;
                    dqbl = -1;
                    if (!dqwz.equals(" ")) {
                        setbtn2(dqwz, false);
                        dqwz = " ";
                    }
                }
                break;
            case R.id.pad_qt:
                if (dqlx != 5) {
                    mData.clear();
                    mData.addAll(mData5);
                    adapter.notifyDataSetChanged();
                    qt.setBackgroundResource(R.drawable.shape_bg5);
                    setbtnys();
                    dqlx = 5;
                    dqbl = -1;
                    if (!dqwz.equals(" ")) {
                        setbtn2(dqwz, false);
                        dqwz = " ";
                    }
                }
                break;
            case R.id.pad_cz:
                onCZ();

                break;
            case R.id.pad_qr:
                String str = "";
                String sstr = "";
                String ssurl = "";
                if (!dqwz.equals(" ")) {
                    setbtn2(dqwz, false);
                    dqwz = "";
                }
                dqbl = -1;
			/* for (Badlynetity  b : mData0)
			 {
				 if(b.getZt().equals("1"))
				 {
					 str=str+"#"+b.getName()+"-"+b.getLeixing();
					 sstr=sstr+"#"+b.getWeizhi();
					 len++;
				 }
			 }*/
//
                if (lastWeizhi.length() < 2) {
                    lastWeizhi = " ";
                }
                for (Badlynetity b : mData1) {
                    if (b.getZt().equals("1")) {
                        str = str + "#" + b.getName() + "-" + b.getNameen() + "-" + b.getNamevi() + "-" + b.getLeixing();
                        String weizhi = b.getWeizhi();
                        if (weizhi.length() < 2) {
                            weizhi = lastWeizhi;
                        }
                        sstr = sstr + "#" + weizhi;
                        len++;
                    }
                }
                for (Badlynetity b : mData2) {
                    if (b.getZt().equals("1")) {
                        str = str + "#" + b.getName() + "-" + b.getNameen() + "-" + b.getNamevi() + "-" + b.getLeixing();
                        String weizhi = b.getWeizhi();
                        if (weizhi.length() < 2) {
                            weizhi = lastWeizhi;
                        }
                        sstr = sstr + "#" + weizhi;
                        len++;
                    }
                }
                for (Badlynetity b : mData3) {
                    if (b.getZt().equals("1")) {
                        str = str + "#" + b.getName() + "-" + b.getNameen() + "-" + b.getNamevi() + "-" + b.getLeixing();
                        String weizhi = b.getWeizhi();
                        if (weizhi.length() < 2) {
                            weizhi = lastWeizhi;
                        }
                        sstr = sstr + "#" + weizhi;
                        len++;
                    }
                }
                for (Badlynetity b : mData4) {
                    if (b.getZt().equals("1")) {
                        str = str + "#" + b.getName() + "-" + b.getNameen() + "-" + b.getNamevi() + "-" + b.getLeixing();
                        String weizhi = b.getWeizhi();
                        if (weizhi.length() < 2) {
                            weizhi = lastWeizhi;
                        }
                        sstr = sstr + "#" + weizhi;
                        len++;
                    }
                }
                for (Badlynetity b : mData5) {
                    if (b.getZt().equals("1")) {
                        str = str + "#" + b.getName() + "-" + b.getNameen() + "-" + b.getNamevi() + "-" + b.getLeixing();
                        String weizhi = b.getWeizhi();
                        if (weizhi.length() < 2) {
                            weizhi = lastWeizhi;
                        }
                        sstr = sstr + "#" + weizhi;
                        len++;
                    }
                }
                if (po == null || po.length() < 2) {
                    len = 0;
                    Utils.showToast(getString(R.string.fail));
                    onCZ();
                } else  if (len > 0) {
                    lastWeizhi = "";
                    str = str.substring(1);
                    sstr = sstr.substring(1);
                    try {
                        str = URLEncoder.encode(str, "UTF-8");
                        sstr = URLEncoder.encode(sstr, "UTF-8");
                        jianchu++;
                        ssurl = surl + "?sn=" + sn + "&&po=" + po + "&&style=" + style + "&&num=" + jianchu + "&&data=" + str + "&&weizhi=" + sstr;
                        setData(ssurl);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.pad_ycs:
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtra("sn", sn);
                startActivity(intent);
                break;
            case R.id.Pad_zjs:
                Intent intent1 = new Intent(MainActivity.this, Second2Activity.class);
                startActivity(intent1);
                break;
            case R.id.pad_cx:
                break;
            case R.id.tv_line:
                //调用相册
                Intent intent5 = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent5, 1);
                break;

        }

    }

    private void onCZ() {
        if (!dqwz.equals("")) {
            setbtn2(dqwz, false);
            dqwz = " ";
        }
        for (Badlynetity b : mData0) {
            b.setZt("0");
            b.setWeizhi(" ");
        }
        for (Badlynetity b : mData1) {
            b.setWeizhi(" ");
            b.setZt("0");
        }
        for (Badlynetity b : mData2) {
            b.setZt("0");
            b.setWeizhi(" ");
        }

        for (Badlynetity b : mData3) {
            b.setZt("0");
            b.setWeizhi(" ");
        }
        for (Badlynetity b : mData4) {
            b.setZt("0");
            b.setWeizhi(" ");
        }
        for (Badlynetity b : mData5) {
            b.setZt("0");
            b.setWeizhi(" ");
        }
        adapter.notifyDataSetChanged();
        dqbl = -1;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //获取图片路径
        if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
            Uri selectedImage = data.getData();
            String[] filePathColumns = {MediaStore.Images.Media.DATA};
            Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePathColumns[0]);
            String imagePath = c.getString(columnIndex);
            MyApplication.getSharedUserInfo().edit().putString("path", imagePath).commit();
            showImage(imagePath);
            c.close();
        }
    }

    private void showImage(String imaePath) {

        Bitmap bm = null;

        if (new File(imaePath).exists()) {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inJustDecodeBounds = false;
            bm = BitmapFactory.decodeFile(imaePath, opts);
        }

        if (bm == null) {
            bg.setBackgroundResource(R.drawable.log);
        } else {
            bg.setBackground(new BitmapDrawable(getResources(), bm));
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                long secondTime = System.currentTimeMillis();
                if (secondTime - firstTime > 2000) { //如果两次按键时间间隔大于2秒，则不退出
                    Toast.makeText(this, getString(R.string.zayctc), Toast.LENGTH_SHORT).show();
                    firstTime = secondTime;// 更新firstTime
                    return true;
                } else { //两次按键小于2秒时，退出应用
                    this.finish();
                    MyApplication.exit();
                }
                break;
        }
        return super.onKeyUp(keyCode, event);
    }
}
